package com.fasteat.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasteat.common.entity.Result;
import com.fasteat.mall.entity.Advertisement;
import com.fasteat.mall.service.AdvertisementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ghost
 * @since 2021-01-19
 */
@RestController
@RequestMapping("/mall/advertisement")
@Api(description = "广告后台接口")
@CrossOrigin
public class AdvertisementController {

	@Autowired
	private AdvertisementService advertisementService;


	/**
	 * 发布广告
	 *
	 * @param advertisement
	 * @return
	 */
	@PostMapping
	@ApiOperation("发布广告")
	public Result publishAdvertisement(@RequestBody Advertisement advertisement) {
		if (StringUtils.isEmpty(advertisement.getContent())
				&& StringUtils.isEmpty(advertisement.getPicture())) {
			return Result.error();
		}
		// 数据库保存消息
		advertisementService.save(advertisement);
		return Result.success();
	}


	/**
	 * 分页查看广告
	 *
	 * @return
	 */
	@GetMapping("/{size}/{current}")
	@ApiOperation("分页查看广告")
	public Result acceptAdvertisement(@PathVariable(name = "size") @ApiParam(required = true, value = "尺寸") int size,
									  @PathVariable(name = "current") @ApiParam(required = true, value = "当前页码") int current) {
		size = size <= 0 ? 5 : size;
		current = current <= 0 ? 1 : current;
		Page<Advertisement> page = new Page(current, size);
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.orderByDesc("gmt_create");
		advertisementService.page(page, wrapper);
		return Result.success().data(page);
	}


	/**
	 * 修改广告内容
	 * @param advertisement
	 * @return
	 */
	@PutMapping
	@ApiOperation("修改广告内容")
	public Result updateAdvertisement(@RequestBody @ApiParam(value = "广告对象") Advertisement advertisement) {
		if (StringUtils.isEmpty(advertisement.getId())) {
			return Result.error();
		}
		boolean ret = advertisementService.updateById(advertisement);
		return ret ? Result.success("修改成功") : Result.error("修改失败");
	}

	/**
	 * 通过广告ID删除广告
	 * @param id
	 * @return
	 */
	@DeleteMapping("/{id}")
	@ApiOperation("删除广告")
	public Result deleteAdvertisement(@PathVariable(name = "id") @ApiParam("广告ID") String id) {
		if (StringUtils.isEmpty(id)) {
			return Result.error();
		}
		boolean ret = advertisementService.removeById(id);
		return ret ? Result.success("删除成功") : Result.error("删除失败");
	}

	/**
	 * 分页查看广告
	 *
	 * @return
	 */
	@GetMapping("/like/{size}/{current}")
	@ApiOperation("搜索广告")
	public Result acceptAdvertisementlike(@PathVariable(name = "size") @ApiParam(required = true, value = "尺寸") int size,
									  @PathVariable(name = "current") @ApiParam(required = true, value = "当前页码") int current,
										  String key
	) {
		size = size <= 0 ? 5 : size;
		current = current <= 0 ? 1 : current;
		Page<Advertisement> page = new Page(current, size);
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.like("content", key);
		advertisementService.page(page, wrapper);
		return Result.success().data(page);
	}
}

